//
//  XXJALLTableViewController.m
//  gosh
//
//  Created by 许小军 on 16/9/25.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#import "XXJALLTableViewController.h"
#import <MJRefresh.h>
#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import "XXJAllModel.h"
#import <SVProgressHUD.h>
#import "XXJALLCell.h"
@interface XXJALLTableViewController ()
{
    
    NSMutableArray * _dataArr;
    NSString * _maxtime;
}
//@property(strong,nonatomic)NSMutableArray * dataArr;
@end
static NSString * const CELLID=@"CELL";
@implementation XXJALLTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor lightGrayColor];
    
    self.tableView.contentInset=UIEdgeInsetsMake(99, 0, 49, 0);
    
    //XXJTabBar 按钮双击通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tabBarClicked) name:TabBarDoubleClicked object:nil];
    
    //titleBtn的双击通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(titleBtnClick) name:TitleBtnDoubleClicked object:nil];
    
    [self setUpRefresh];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XXJALLCell class]) bundle:nil] forCellReuseIdentifier:CELLID];
}

-(void)setUpRefresh
{
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadDataHeader)];
    self.tableView.mj_header.automaticallyChangeAlpha=YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshHeader)];
}
-(void)loadDataHeader
{
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    NSMutableDictionary * dic=[NSMutableDictionary dictionary];
    dic[@"a"]=@"list";
    dic[@"c"]=@"data";
    dic[@"type"]=@31;
    
    [manager GET:@"http://api.budejie.com/api/api_open.php" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        _maxtime=responseObject[@"info"][@"maxtime"];
        NSMutableArray * array=responseObject[@"list"];
        _dataArr=[XXJAllModel mj_objectArrayWithKeyValuesArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络连接异常，请稍后再试..."];
    }];
    
    
    
    
}

-(void)footerRefreshHeader
{
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    NSMutableDictionary * dic=[NSMutableDictionary dictionary];
    dic[@"a"]=@"list";
    dic[@"c"]=@"data";
    dic[@"type"]=@31;
    dic[@"maxtime"]=@"_maxtime";
    
    [manager GET:@"http://api.budejie.com/api/api_open.php" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        _maxtime=responseObject[@"info"][@"maxtime"];
        NSMutableArray * array=responseObject[@"list"];
        NSMutableArray * modelArr=[XXJAllModel mj_objectArrayWithKeyValuesArray:array];
        [_dataArr addObjectsFromArray:modelArr];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络连接异常，请稍后再试..."];
    }];
}


-(void)tabBarClicked
{
    if (self.view.window==nil)//判断如果点击别的tabbar按钮，对应的视图窗口是不是在精华上
    {
        return;
    }
    if (self.tableView.scrollsToTop==NO)//判断精华里面的子视图在窗口上的的是哪一个
    {
        return;
    }
    [self.tableView.mj_header beginRefreshing];
    
}

-(void)titleBtnClick
{
    if (self.view.window==nil) {
        return;
    }
    if (self.tableView.scrollsToTop==NO) {
        return;
    }
    [self.tableView.mj_header beginRefreshing];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XXJALLCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
//    if (cell==nil)
//    {
//        cell=[[XXJALLCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELLID];
//    }
    cell.backgroundColor=[UIColor clearColor];
    
    XXJAllModel *model=_dataArr[indexPath.row];
    cell.model=model;
   // cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //XXJAllModel * model=_dataArr[indexPath.row];
    return 200;
}



@end
